package com.test;



import sun.font.FontDesignMetrics;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtils {
    /**
     *
     * @param x1 背景图左上角坐标x轴
     * @param y1 背景图左上角坐标y轴
     * @param x2 背景图右下角坐标x轴
     * @param y2 背景图右下角坐标y轴
     * @param backgroundPath 背景图路径
     * @param frameworkPath 框架图路径
     * @param uiPath ui图路径
     * @param outPath 合成图片后输出的路径
     */
    public static void margeImage(int x1, int y1, int x2, int y2, String backgroundPath, String frameworkPath, String uiPath, String outPath, String fileNameStr, String year) throws IOException {
        System.out.println(backgroundPath+",,,backgroundPath");
        File file = new File(backgroundPath);
        Image img = ImageIO.read(file);
        //背景图宽度
        int width = img.getWidth(null);
        //背景图高度
        int height = img.getHeight(null);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();



        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        //透明度
        float clarity = 1.0f;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, clarity));

        //背景图定位的框架宽度
        int uXSize = x2 - x1;
        //背景图定位的框架长度
        int uYSize = y2 - y1;

        //设置框架的相关属性
        File frameworkFile = new File(frameworkPath);
        String frameworkFileName = frameworkFile.getName();
        String[] frameworkStrings = frameworkFileName.split("_");
        System.out.println(frameworkFileName+",,,,,");
        //获取框架里的ui定位点
        String frameworkName = null;
        int frameworkX1 = 0;
        int frameworkY1 = 0;
        int frameworkX2 = 0;
        int frameworkY2 = 0;
        if(frameworkStrings[0] != null){
            frameworkName = frameworkStrings[0];
            System.out.println(frameworkStrings[0]+",,,,frameworkStrings[0]");
        }
        if(frameworkStrings[1] != null){
            frameworkX1 = Integer.parseInt(frameworkStrings[1]);
            System.out.println(frameworkStrings[1]+",,,,,frameworkStrings[2]");
        }
        if(frameworkStrings[2] != null){
            frameworkY1 = Integer.parseInt(frameworkStrings[2]);
            System.out.println(frameworkStrings[2]+",,,,frameworkStrings[3]");
        }
        if(frameworkStrings[3] != null){
            frameworkX2 = Integer.parseInt(frameworkStrings[3]);
            System.out.println(frameworkStrings[3]+",,,,frameworkStrings[4]");
        }
        if(frameworkStrings[4] != null){
            frameworkY2 = Integer.parseInt(frameworkStrings[4]);
            System.out.println(frameworkStrings[4]+",,,,frameworkStrings[5]");
        }

        //缩放前框架里的ui图片的宽度
        int frameworkUiXSize = frameworkX2 - frameworkX1;
        //缩放前框架里的ui图片的高度
        int frameworkUiYSize = frameworkY2 - frameworkY1;

        ImageIcon frameworkImgIcon = new ImageIcon(frameworkPath);

        //框架的宽度
        int frameworkImgIconWidth = frameworkImgIcon.getIconWidth();
        //框架的高度
        int frameworkImgIconHeight = frameworkImgIcon.getIconHeight();

        //设置ui图相关属性
        ImageIcon uiImgIcon = new ImageIcon(uiPath);


        //将框架图进行缩小或放大
        int x = 0;
        int y = 0;
        int ux13 = 0;
        int uy13 = 0;
        int xI = 0;
        int yI = 0;
        int ux1 = 0;
        int uy1 = 0;
        if(frameworkImgIconWidth > uXSize && frameworkImgIconHeight > uYSize){
            System.out.println("1");
            double xc = (double)frameworkImgIconWidth - (double)uXSize;
            double yc = (double)frameworkImgIconHeight - (double)uYSize;
            double il = (double)frameworkImgIconWidth/(double)frameworkImgIconHeight;
            double cl = xc/yc;
            if(cl >= il){
                //将长缩小到和背景定位的长一致
                xI = uXSize;
                yI = (int) (frameworkImgIconHeight - (xc/il));
                ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
                uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
                ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
                uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

            }else {
                //将宽缩小到和背景定位的宽一致
                yI = uYSize;
                //xI = ((double)frameworkImgIconWidth/frameworkImgIconHeight)*yI;
                xI = (int)((double)frameworkImgIconWidth/(double)frameworkImgIconHeight*uYSize);
                //xI = (int)(frameworkImgIconWidth-((frameworkImgIconHeight-uYSize)*cl));
                ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
                uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
                ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
                uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);
            }
        }
        if(frameworkImgIconWidth > uXSize && frameworkImgIconHeight == uYSize){
            System.out.println("2");
            double xc = (double)frameworkImgIconWidth - (double)uXSize;
            double yc = (double)frameworkImgIconHeight - (double)uYSize;
            double il = (double)frameworkImgIconWidth/(double)frameworkImgIconHeight;
            double cl = xc/yc;
            //将长缩小到和背景定位的长一致
            xI = uXSize;
            yI = (int) (frameworkImgIconHeight - (xc/il));
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

        }
        if(frameworkImgIconWidth > uXSize && frameworkImgIconHeight < uYSize){
            System.out.println("3");
            double xc = (double)frameworkImgIconWidth - (double)uXSize;
            double yc = (double)frameworkImgIconHeight - (double)uYSize;
            double il = (double)frameworkImgIconWidth/(double)frameworkImgIconHeight;
            double cl = xc/yc;
            //将长缩小到和背景定位的长一致
            xI = uXSize;
            yI = (int) (frameworkImgIconHeight - (xc/il));
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

        }
        if(frameworkImgIconWidth == uXSize && frameworkImgIconHeight > uYSize){
            System.out.println("4");
            double xc = (double)frameworkImgIconWidth - (double)uXSize;
            double yc = (double)frameworkImgIconHeight - (double)uYSize;
            double il = (double)frameworkImgIconWidth/(double)frameworkImgIconHeight;
            double cl = xc/yc;
            yI = uYSize;
            xI = (int)(frameworkImgIconWidth - (yc/il));
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

        }
        if(frameworkImgIconWidth == uXSize && frameworkImgIconHeight == uYSize){
            System.out.println("5");
            xI = uXSize;
            yI = uYSize;
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

        }
        if(frameworkImgIconWidth == uXSize && frameworkImgIconHeight < uYSize){
            System.out.println("6");
            xI = uXSize;
            yI = uYSize;
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);
        }
        if(frameworkImgIconWidth < uXSize && frameworkImgIconHeight > uYSize){
            System.out.println("7");
            double xc = (double)frameworkImgIconWidth - (double)uXSize;
            double yc = (double)frameworkImgIconHeight - (double)uYSize;
            double il = (double)frameworkImgIconWidth/(double)frameworkImgIconHeight;
            double cl = xc/yc;
            yI = uYSize;
            xI = (int)(frameworkImgIconWidth - ((frameworkImgIconHeight-uYSize)*il));
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

        }
        if(frameworkImgIconWidth < uXSize && frameworkImgIconHeight == uYSize){
            System.out.println("8");
            xI = uXSize;
            yI = uYSize;
            ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
            uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
            ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
            uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);
        }
        if(frameworkImgIconWidth < uXSize && frameworkImgIconHeight < uYSize){
            System.out.println("9");
            double xc = (double)uXSize;
            double yc = (double)uYSize;
            double il = (double)frameworkImgIconWidth/(double)frameworkImgIconHeight;
            double cl = xc/yc;
            if(cl <= il){
                System.out.println("10");
                //将长缩小到和背景定位的长一致
                xI = uXSize;
                yI = (int) (frameworkImgIconHeight+((uXSize-frameworkImgIconWidth)*(yc/xc)));
                ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
                uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
                ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
                uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);

            }else {
                System.out.println("11");
                //将宽缩小到和背景定位的宽一致
                yI = uYSize;
                xI = (int)(frameworkImgIconWidth + ((uYSize-frameworkImgIconHeight)*cl));
                ux1 = (int) (frameworkUiXSize * xI/frameworkImgIconWidth);
                uy1 = (int) (frameworkUiYSize * yI/frameworkImgIconHeight);
                ux13 = (int)(frameworkX1 * xI/frameworkImgIconWidth);
                uy13 = (int)(frameworkY1 * yI/frameworkImgIconHeight);
            }
        }
        frameworkImgIcon.setImage(frameworkImgIcon.getImage().getScaledInstance(xI,yI,Image.SCALE_DEFAULT));
        uiImgIcon.setImage(uiImgIcon.getImage().getScaledInstance(ux1,uy1,Image.SCALE_DEFAULT));
//            g.drawImage(con, x1, y1, null);
        Image frameworkCon = frameworkImgIcon.getImage();
        Image uiCon = uiImgIcon.getImage();
        x = (uXSize-xI)/2+x1;
        y = y1+((uYSize-yI)/2);
        //水印的位置
        g.drawImage(uiCon,x+ux13, y+uy13, null);
        g.drawImage(frameworkCon, x, y, null);

        //设置字体
        int sizeFront = 50;
        Font font = null;
        int fileNameStrWidth = 0;
        font = new Font("微软雅黑",Font.BOLD,sizeFront);
        //计算字体的宽度
        fileNameStrWidth = ImageUtils.getWordWidth(font, fileNameStr);
        while (fileNameStrWidth > (width-40)){
            sizeFront--;
            font = new Font("微软雅黑",Font.BOLD,sizeFront);
            fileNameStrWidth = ImageUtils.getWordWidth(font, fileNameStr);
        }
        g.setFont(font);
        //Color color2 = new Color(255,255,255);
        //g.setColor(color2);
        g.setColor(new Color(222,184,135));


        g.drawString(fileNameStr, width-(fileNameStrWidth+20), 100);
        font = new Font("微软雅黑",Font.BOLD,50);
        g.setFont(font);
        //计算字体的宽度
        int yearWidth = ImageUtils.getWordWidth(font, year);
        g.drawString(year,width-(yearWidth+20),150);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();
        File sf = new File(outPath);
// 保存图片
        ImageIO.write(bi, "png", sf);
    }

    public static int getWordWidth(Font font, String content) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = 0;
        for (int i = 0; i < content.length(); i++) {
            width += metrics.charWidth(content.charAt(i));
        }
        return width;
    }


}
